Python program to input a string and find total number uppercase and lowercase letters

Here, we are going to learn how to find the total number of uppercase and lowercase letters in a given string in Python programming language? By IncludeHelp Last updated : February 25, 2024

Problem statement

Given a string str1 and we have to count the total numbers of uppercase and lowercase letters.

Example

Input: 
"Hello World!"
Output:
Uppercase letters: 2
Lowercase letters: 8

Input: 
"Hello@123"
Output:
Uppercase letters: 1
Lowercase letters: 4

Using Loop By Checking Each Character

(Manual) By checking each character of the string with a range of uppercase and lowercase letters using the conditional statement.

print("Input a string: ")
str1 = input()

no_of_ucase, no_of_lcase = 0,0

for c in str1:
    if c>='A' and c<='Z':
        no_of_ucase += 1
    if c>='a' and c<='z':
        no_of_lcase += 1

print("Input string is: ", str1)
print("Total number of uppercase letters: ", no_of_ucase)
print("Total number of lowercase letters: ", no_of_lcase)

Output

RUN 1:
Input a string: 
Hello World!
Input string is:  Hello World!
Total number of uppercase letters:  2
Total number of lowercase letters:  8

RUN 2:
nput a string: 
Hello@123
Input string is:  Hello@123
Total number of uppercase letters:  1
Total number of lowercase letters:  4

Using islower() and isupper() Methods

By using islower() and isupper() methods

print("Input a string: ")
str1 = input()

no_of_ucase, no_of_lcase = 0,0

for c in str1:
  no_of_ucase += c.isupper()
  no_of_lcase += c.islower()

print("Input string is: ", str1)
print("Total number of uppercase letters: ", no_of_ucase)
print("Total number of lowercase letters: ", no_of_lcase)

Output

RUN 1:
Input a string: 
Hello World!
Input string is:  Hello World!
Total number of uppercase letters:  2
Total number of lowercase letters:  8

RUN 2:
nput a string: 
Hello@123
Input string is:  Hello@123
Total number of uppercase letters:  1
Total number of lowercase letters:  4

To understand the above programs, you should have the basic knowledge of the following Python topics:

Python String Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.